<?php
/***************************************************************************
 *
 *   copyright            : (C) 2005 Winds of Storm
 *
 *   $Id: options.php,v 1.13 2007/06/03 06:49:10 amorgan Exp $
 *
 ***************************************************************************/

/***************************************************************************
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 ***************************************************************************/

require("config.php");
$full_header			= 1;
$body['class']			= 'row3';
$header[]['HEADER_TEXT']	= '<script language=JavaScript src="picker.js"></script>';
require($libdir . "init." . $phpEx);

$sql    = "SELECT * FROM {$dbprefix}sessions WHERE session_id='{$_REQUEST['sid']}'";
$result = $db->sql_query($sql);
$row    = $db->sql_fetchrow($result);
if (!is_array($row)) {
	showFatalError("Please <a href=index.$phpEx>log back in</a> first.");
}

$sql    = "SELECT * FROM {$dbprefix}prefs WHERE user_id={$row['user_id']}";
$result = $db->sql_query($sql);
$p_row  = $db->sql_fetchrow($result);

if ($_REQUEST['user_id']) {
	if (is_array($p_row)) {
		$sql	= "UPDATE {$dbprefix}prefs
				SET
					user_tagline='{$_REQUEST['user_tagline']}',
					user_login_msg='{$_REQUEST['user_login_msg']}',
					user_logout_msg='{$_REQUEST['user_logout_msg']}',
					user_show_avatars='{$_REQUEST['user_show_avatars']}',
					user_allow_pms='{$_REQUEST['user_allow_pms']}',
					user_allow_ext_pms='{$_REQUEST['user_allow_ext_pms']}',
					user_show_smilies='{$_REQUEST['user_show_smilies']}',
					user_color='{$_REQUEST['user_color']}',
					user_css='{$_REQUEST['user_css']}'
				WHERE
					user_id={$row['user_id']}";
		$notice	= "Your preferences have been updated."; 
	} else {
		$sql	= "INSERT INTO {$dbprefix}prefs
					(user_id, user_tagline, user_login_msg, user_logout_msg, user_color, user_css)
				VALUES
					({$_REQUEST['user_id']}, '{$_REQUEST['user_tagline']}', '{$_REQUEST['user_login_msg']}', '{$_REQUEST['user_logout_msg']}', '{$_REQUEST['user_color']}', '{$_REQUEST['user_css']}')";
		$notice	= "Your preferences have been created.";
	}
	$result = $db->sql_query($sql);
	//$err    = $db->sql_error();

	$sql    = "SELECT * FROM {$dbprefix}prefs WHERE user_id={$row['user_id']}";
	$result = $db->sql_query($sql);
	$p_row  = $db->sql_fetchrow($result);
}

$template = new Template($config['TPL_DIR']);
$template->set_filenames(array('body' => 'options_body.tpl'));

$y_arr	= array('Y' => 'checked', 'N' => '');
$n_arr	= array('Y' => '', 'N' => 'checked');

$c_avatars	= ($p_row['user_show_avatars']	== 0 ? $y_arr : $n_arr);
$c_smilies	= ($p_row['user_show_smilies']	== 0 ? $y_arr : $n_arr);
$c_pms		= ($p_row['user_allow_pms']	== 0 ? $y_arr : $n_arr);
$c_ext_pms	= ($p_row['user_allow_ext_pms']	== 0 ? $y_arr : $n_arr);

$template->assign_vars(array(
	'USER_ID'		=> $row['user_id'],
	'SID'			=> $_REQUEST['sid'],

	'USER_TAGLINE'		=> $p_row['user_tagline'],
	'USER_LOGIN_MSG'	=> $p_row['user_login_msg'],
	'USER_LOGOUT_MSG'	=> $p_row['user_logout_msg'],
	'USER_COLOR'		=> $p_row['user_color'],
	'USER_CSS'		=> $p_row['user_css'],

	'USER_SHOW_AVATARS_Y'	=> $c_avatars['Y'],
	'USER_SHOW_AVATARS_N'	=> $c_avatars['N'],

	'USER_SHOW_SMILIES_Y'	=> $c_smilies['Y'],
	'USER_SHOW_SMILIES_N'	=> $c_smilies['N'],

	'USER_ALLOW_PMS_Y'	=> $c_pms['Y'],
	'USER_ALLOW_PMS_N'	=> $c_pms['N'],

	'USER_ALLOW_EXT_PMS_Y'	=> $c_ext_pms['Y'],
	'USER_ALLOW_EXT_PMS_N'	=> $c_ext_pms['N']
));

if (isset($notice)) {
	$template->assign_block_vars('enotice', array('TEXT' => $notice));
}

$template->pparse('body');

